<template>
  <div class="menu">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" @click="addMenu">添加菜单</el-button>
      </div>
      <el-table :data="menus" style="width: 100%" height="550">
        <el-table-column type="index" label="编号" width="50">
        </el-table-column>
        <el-table-column prop="name" label="菜单名称"> </el-table-column>
        <el-table-column prop="level" label="菜单级数"> </el-table-column>
        <el-table-column prop="icon" label="前端图标"> </el-table-column>
        <el-table-column prop="orderNum" label="排序"> </el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              @click="handleEdit(scope.$index, scope.row)"
              type="text"
              size="small"
              >编辑</el-button
            >
            <el-button
              @click="handleDelete(scope.$index, scope.row)"
              type="text"
              size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { Menu, getAllMenus, deleteMenu } from '@/services/menu'

export default Vue.extend({
  name: 'Home',
  data () {
    return {
      menus: []
    }
  },
  created () {
    this.loadAllMenus()
  },
  methods: {
    addMenu () {
      this.$router.push({ name: 'menu-create' })
    },

    async loadAllMenus () {
      const { data } = await getAllMenus()
      console.log(data)
      if (data.code === '000000') {
        this.menus = data.data
      }
    },

    handleEdit (index: number, item: Menu): void {
      console.log(index, item)
      this.$router.push({ name: 'menu-edit', params: { id: String(item.id) } })
    },

    handleDelete (index: number, item: Menu) {
      console.log(index, item)
      this.$confirm('确认删除吗？', '删除提示').then(async () => {
        const { data } = await deleteMenu(item.id)
        if (data.code === '000000') {
          this.$message.success('删除成功')
          this.loadAllMenus()
        }
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    }
  }
})
</script>

<style lang="scss" scoped>
</style>
